<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="isDialog = true">添加角色</el-button>

      </div>

      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色" prop="name">
          <template v-slot="{ row }">
            <!-- 条件判断 -->
            <el-input v-if="row.isEdit" v-model="row.editRow.name" size="mini" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" label="启用" prop="state">

          <template v-slot="{ row }">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>  {{ row.state === 1 ? "已启用" : row.state === 0 ? "未启用" : "无" }} </span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="描述" prop="description">
          <template v-slot="{ row }">
            <!-- 条件判断 -->
            <el-input v-if="row.isEdit" v-model="row.editRow.description" size="mini" />
            <span v-else>{{ row.description }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <template v-if="row.isEdit">
              <!-- 编辑状态 -->
              <el-button type="primary" size="mini" @click="confirm(row)">确定</el-button>

              <el-button size="mini" @click="row.isEdit = false">取消</el-button>

            </template>

            <template v-else>
              <!-- 非编辑状态 -->
              <el-button size="mini" type="text">分配权限</el-button>

              <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>

              <el-popconfirm title="这是一段内容确定删除吗？" @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" size="mini" type="text">删除</el-button>

              </el-popconfirm>

            </template>
          </template>
        </el-table-column>
      </el-table>

      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="center">
        <!-- 放置分页组件 -->
        <el-pagination
          layout="prev, pager, next"
          :page-size="5"
          :total="total"
          @current-change="changePage"
        />
      </el-row>

    </div>
    <!-- 对话框 -->
    <el-dialog
      title="新增角色"
      :visible.sync="isDialog"
      width="500px"
      @close="closeDialog"
    >
      <!--添加表单-->
      <el-form ref="addFormRef" label-width="120px" :model="addForm" :rules="addrules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="addForm.name" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item label="启用">
          <el-switch v-model="addForm.state" size="mini" :active-value="1" :inactive-value="0" />
        </el-form-item>

        <el-form-item label="角色描述" prop="description">
          <el-input v-model="addForm.description" type="textarea" :rows="3" style="width:300px" size="mini" />
        </el-form-item>

        <el-form-item>
          <el-row type="flex" justify="center">
            <el-col :span="12">
              <el-button type="primary" size="mini" @click="addRole">确定</el-button>

              <el-button size="mini" @click="closeDialog">取消</el-button>

            </el-col>

          </el-row>

        </el-form-item>

      </el-form>
    </el-dialog>
  </div>

</template>
<script>
import { addRoleAPI, delRoleAPI, editRoleAPI, getRoleListAPI } from '@/api/role'

export default {
  data() {
    return {
      // 角色列表数据
      list: [],
      // 全部数量
      total: 0,
      // 是否编辑
      isEdit: true,
      params: {
        page: 1,
        pagesize: 5
      },
      // 是否显示弹窗
      isDialog: false,
      // 表单数据 -- 双向绑定数据
      addForm: {
        name: '',
        state: 0,
        description: ''
      },
      // 添加表单规则
      addrules: {
        name: [
          { required: true, message: '角色名称不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '角色描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取数据
    async getRoleList() {
      const res = await getRoleListAPI(this.params)
      this.list = res.rows
      this.total = res.total
      this.list.forEach(item => {
        // 添加是否正在编辑属性
        this.$set(item, 'isEdit', false)
        // 拷贝一份数据
        this.$set(item, 'editRow', {
          name: item.name,
          state: item.state,
          description: item.description
        })
      })
    },
    // 页数切换，获取数据从新发送请求进行渲染
    changePage(value) {
      this.params.page = value
      this.getRoleList()
    },
    // 添加角色
    addRole() {
      // 表单效验
      this.$refs.addFormRef.validate(async(valid) => {
        if (valid) {
          // 新增角色
          await addRoleAPI(this.addForm)
          // 关闭弹窗
          this.closeDialog()
          // 提示用户
          this.$message.success('新增角色成功')
          // 刷新列表
          this.getRoleList()
        }
      })
    },
    // 关闭弹窗
    closeDialog() {
      this.isDialog = false
      // 重置表单
      this.$refs.addFormRef.resetFields()
      // 数据重置
      this.addForm = {
        name: '',
        state: 0,
        description: ''
      }
    },
    // 行内编辑
    edit(row) {
      row.isEdit = true
      // 更新缓存数据
      row.editRow.name = row.name
      row.editRow.state = !!row.state
      row.editRow.description = row.description
    },
    // 编辑确认
    async confirm(row) {
      if (row.editRow.name && row.editRow.description) {
        // 下一步操作
        await editRoleAPI({ ...row.editRow, id: row.id })
        // 更新成功
        this.$message.success('更新角色成功')
        // 更新显示数据  退出编辑状态
        // row.name = row.editRow.name // eslint的一校验 误判
        // Object.assign(target, source)
        Object.assign(row, {
          ...row.editRow,
          isEdit: false // 退出编辑模式
        }) // 规避eslint的误判
      } else {
        this.$message.warning('角色和描述不能为空')
      }
    },
    // 确认删除
    async confirmDel(id) {
      await delRoleAPI(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) {
        this.params.page--
      }
      this.getRoleList()
    }
  }
}
</script>
<style scoped>
  .role-operate {
  padding: 10px;
}
</style>

